﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TimeTable.Domain.DataAccess;
using TimeTable.Domain.Entity;

namespace TimeTable.ReportForms
{
    public partial class TimeTableForm : Form
    {
        private readonly StudyUnit _studyUnit;

        public TimeTableForm(StudyUnit studyUnit)
        {
            InitializeComponent();

            _studyUnit = studyUnit;

            Text = string.Format("Time table of {0}", studyUnit);

            var units = DataAccessConnection.Instance.GetTimeTableUnitsByStudyUnit(_studyUnit);

            LoadMonday(units);
            LoadTuesday(units);
            LoadWednesday(units);
            LoadThursday(units);
            LoadFriday(units);
            LoadSaturday(units);
        }

        private void LoadMonday(IEnumerable<TimeTableUnit> units)
        {
            var mondayUnits = GetUnitsByDayOfWeek(units, DayOfWeek.Monday);
            mondayUnits.Sort();
            mondayListBox.DataSource = mondayUnits;
        }

        private void LoadTuesday(IEnumerable<TimeTableUnit> units)
        {
            var tuesdayUnits = GetUnitsByDayOfWeek(units, DayOfWeek.Tuesday);
            tuesdayUnits.Sort();
            tuesdayListBox.DataSource = tuesdayUnits;
        }

        private void LoadWednesday(IEnumerable<TimeTableUnit> units)
        {
            var wednesdayUnits = GetUnitsByDayOfWeek(units, DayOfWeek.Wednesday);
            wednesdayUnits.Sort();
            wednesdayListBox.DataSource = wednesdayUnits;
        }

        private void LoadThursday(IEnumerable<TimeTableUnit> units)
        {
            var thursdayUnits = GetUnitsByDayOfWeek(units, DayOfWeek.Thursday);
            thursdayUnits.Sort();
            thursdayListBox.DataSource = thursdayUnits;
        }

        private void LoadFriday(IEnumerable<TimeTableUnit> units)
        {
            var fridayUnits = GetUnitsByDayOfWeek(units, DayOfWeek.Friday);
            fridayUnits.Sort();
            fridayListBox.DataSource = fridayUnits;
        }

        private void LoadSaturday(IEnumerable<TimeTableUnit> units)
        {
            var saturdayUnits = GetUnitsByDayOfWeek(units, DayOfWeek.Saturday);
            saturdayUnits.Sort();
            saturdayListBox.DataSource = saturdayUnits;
        }

        private static List<TimeTableUnit> GetUnitsByDayOfWeek(IEnumerable<TimeTableUnit> units, DayOfWeek dayOfWeek)
        {
            return units.Where(unit => unit.DayOfWeek == dayOfWeek).ToList();
        }
    }
}
